package com.leetroll.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leetroll.api.mapper.CartMapper;
import com.leetroll.api.service.CartService;
import com.leetroll.api.service.FileService;
import com.leetroll.api.service.ItemService;
import com.leetroll.common.constant.SQLConstant;
import com.leetroll.common.entity.Cart;
import com.leetroll.common.entity.Item;
import com.leetroll.common.vo.CartItemVO;
import com.leetroll.common.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class CartServiceImpl extends SuperServiceImpl<CartMapper, Cart> implements CartService {

    private final ItemService itemService;
    private final FileService fileService;

    @Autowired
    public CartServiceImpl(ItemService itemService, FileService fileService) {
        this.itemService = itemService;
        this.fileService = fileService;
    }

    @Override
    public List<CartItemVO> listByUserId(Long userId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConstant.USER_ID, userId);
        List<Cart> cartList = this.list(queryWrapper);
        //组装购物车item资源
        return cartList.stream().map(this::makeCartItem).collect(Collectors.toList());
    }

    @Override
    public boolean exist(Cart cart) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConstant.USER_ID, cart.getUserId());
        queryWrapper.eq(SQLConstant.ITEM_ID, cart.getItemId());
        Cart exsistCart = this.getOne(queryWrapper);
        return exsistCart != null;
    }

    @Override
    public void delByUserIdAndItemId(Long userId, List<Long> itemIds) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConstant.USER_ID, userId);
        queryWrapper.in(SQLConstant.ITEM_ID, itemIds);
        this.remove(queryWrapper);
    }

    @Override
    public List<ItemVO> pageByUserId(Long userId, Integer pageNumber, Integer pageSize) {
        Page<Cart> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConstant.USER_ID, userId);
        List<Cart> cartList = this.page(page, queryWrapper).getRecords();
        return cartList.stream().map(v-> itemService.getById(v.getItemId().intValue())).collect(Collectors.toList());
    }

    private CartItemVO makeCartItem(Cart cart) {
        Item item = itemService.getById(cart.getItemId());
        if (item == null) return null;
        CartItemVO cartItemVO = new CartItemVO();
        cartItemVO.setItemId(item.getId());
        cartItemVO.setAuthor(item.getAuthor());
        cartItemVO.setPrice(item.getPrice());
        cartItemVO.setName(item.getName());

        String main = fileService.getRescMainFile(item.getId());
        cartItemVO.setMain(main);

        cartItemVO.setId(cart.getId());
        return cartItemVO;
    }

}
